﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using VO;
namespace ManagementBooks
{
    public partial class DanhMucSach : Form
    {
        string TenDN;
        public delegate void SendMessage(string TenDangNhap);
        public SendMessage Sender;
        private void GetMessage(string TenDangNhap)
        {
            this.TenDN = TenDangNhap;
        }
        public DanhMucSach()
        {
            InitializeComponent();
             Sender = new SendMessage(GetMessage);
        }
        NhanVien_BUS _NhanvienBUS = new NhanVien_BUS();
        SachBUS sachBUS = new SachBUS();
        private void DanhMucSach_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable dtt = new DataTable();
                dtt = sachBUS.showsach();
                dataGridView1.DataSource = dtt;
            }
            catch { }
        }

        private void tkten_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dtt = new DataTable();
                dtt = sachBUS.timtheoten(tbtensach.Text);
                dataGridView1.DataSource = dtt;

            }
            catch
            {

            }
        }

        private void tramanxb_Click(object sender, EventArgs e)
        {
            NXB nxb = new NXB();
            nxb.Show();
        }
        private void tinhtongtien()
        {
            double soLuong = 0;
            double donGia = 0;
            double thanhTien = 0;
            try
            {
                double.TryParse(tbsoluong.Text, out soLuong);
                double.TryParse(tbdongia.Text, out donGia);
                thanhTien = soLuong * donGia;
                tongtien = String.Format("{0:0,0}", thanhTien.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.Message);

            }

        }
        string tongtien;
        private void tramatheloai_Click(object sender, EventArgs e)
        {
            TheLoai tl = new TheLoai();
            tl.Show();
        }
        KhachHang_BUS _KhachHangBUS = new KhachHang_BUS();
        HoaDon_BUS _HoaDonBUS = new HoaDon_BUS();
        int mahoadon;
        SachBUS _SachBUS = new SachBUS();
        private void them_Click(object sender, EventArgs e)
        {
            try
            {
                tinhtongtien();
                sachBUS.themsach(tbtensach.Text, tbtentg.Text, tbmanxb.Text, tbtheloai.Text, tbdongia.Text, tbsoluong.Text);
                MessageBox.Show("Đã thêm thành công", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                HoaDon_OBJ hdobj = new HoaDon_OBJ();
                hdobj.TongTien = double.Parse(tongtien);
                hdobj.TenDangNhap = TenDN;
                hdobj.MaKhachHang = Convert.ToInt32(_KhachHangBUS.MaKhachHang().Rows[0]["MaKhachHang"].ToString());
                hdobj.KieuHoaDon = "Hoá đơn nhập";
                BUS.HoaDon_BUS.Customer_Insert(hdobj);
                ChiTietHoaDon_OBJ objh = new ChiTietHoaDon_OBJ();
                DataTable dt = new DataTable();
                dt = _HoaDonBUS.MaHoaDon();
                mahoadon = Convert.ToInt32(dt.Rows[0]["MaHoaDon"].ToString());   
                objh.MaHoaDon = mahoadon;
                objh.MaSach = Convert.ToInt32(_SachBUS.LayMaSach().Rows[0]["MaSach"].ToString());
                objh.DonGia = Convert.ToInt32(tbdongia.Text);
                objh.SoLuong = Convert.ToInt32(tbsoluong.Text);
                objh.ThanhTien = Convert.ToInt32(tongtien);
                BUS.ChiTietHoaDon_BUS.Customer_Insert(objh);
            }
            catch
            { }
            DanhMucSach_Load(sender, e);
        }

        private void sua_Click(object sender, EventArgs e)
        {
            try
            {

                sachBUS.suasach(Convert.ToInt32(tbmasach.Text), tbtensach.Text, tbtentg.Text, tbdongia.Text, tbsoluong.Text);
                MessageBox.Show("Đã sửa thành công", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            { }
            DanhMucSach_Load(sender, e);
        }

        private void xoa_Click(object sender, EventArgs e)
        {
            try
            {
                sachBUS.xoasach(tbmasach.Text);
                MessageBox.Show("Đã xóa thành công", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch
            { }
            DanhMucSach_Load(sender, e);
        }

        private void all_Click(object sender, EventArgs e)
        {
            DanhMucSach_Load(sender, e);
        }

        private void thoat_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc muốn thoát hay không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                this.Close();
                Dispose();
            }
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dem = e.RowIndex;
            tbdongia.Text = dataGridView1.Rows[dem].Cells["dongia"].Value.ToString();
            tbmanxb.Text = dataGridView1.Rows[dem].Cells["manxb"].Value.ToString();
            tbsoluong.Text = dataGridView1.Rows[dem].Cells["soluong"].Value.ToString();
            tbtensach.Text = dataGridView1.Rows[dem].Cells["tensach"].Value.ToString();
            tbtentg.Text = dataGridView1.Rows[dem].Cells["tentg"].Value.ToString();
            tbtheloai.Text = dataGridView1.Rows[dem].Cells["matheloai"].Value.ToString();
            tbmasach.Text = dataGridView1.Rows[dem].Cells["masach"].Value.ToString();
        }

        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            int dem = e.RowIndex;
            dataGridView1.Rows[dem].Cells[0].Value = dem + 1;
        }
    }
}
